﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SoucreGame.GUI;
using SoucreGame.DTO;
using SoucreGame.BUS;
using SoucreGame.BUS.CBusiSkill;
using SoucreGame.GLO;

namespace SoucreGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;  
        
        guiLevel guiLV;
        guiWelcome guiWC;
        guiAmThanh guiAT;
        guiAmLuong guiAL;
        guiCaiDat guiCD;
        guiDatTen guiDT;
        guiManHinhNhacNen guiNN;
        guiManHinhThoat guiThoat;
        guiMapGamePlay guiMap;

        CBusiBomNo bomNo;
        CBusiHoiMau hoiMau;
        CBusiLamCham lamCham;
        CBusiNoKiem noKiem;
        CBusiSamSet samSet;
        CBusiSuTuHong suTuHong;
        CBusiThienThach thienThach;
        CBusiTroi troi;
        CBusiXoayNuoc xoayNuoc;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.IsMouseVisible = true;
            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferHeight = 680;
            graphics.PreferredBackBufferWidth = 1360;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            // TODO: use this.Content to load your game content here
            
            guiLV = new guiLevel(Content, null, spriteBatch);
            guiWC = new guiWelcome(Content, null, spriteBatch);
            guiAT = new guiAmThanh(Content, null, spriteBatch);
            guiAL = new guiAmLuong(Content, null, spriteBatch);
            guiCD = new guiCaiDat(Content, null, spriteBatch);
            guiDT = new guiDatTen(Content, null, spriteBatch);
            guiNN = new guiManHinhNhacNen(Content, null, spriteBatch);
            guiThoat = new guiManHinhThoat(Content, null, spriteBatch);
            guiMap = new guiMapGamePlay(Content, null, spriteBatch);

            bomNo = new CBusiBomNo(Content, null, spriteBatch);
            hoiMau = new CBusiHoiMau(Content, null, spriteBatch);
            lamCham = new CBusiLamCham(Content, null, spriteBatch);
            noKiem = new CBusiNoKiem(Content, null, spriteBatch);
            samSet = new CBusiSamSet(Content, null, spriteBatch);
            suTuHong = new CBusiSuTuHong(Content, null, spriteBatch);
            thienThach = new CBusiThienThach(Content, null, spriteBatch);
            troi = new CBusiTroi(Content, null, spriteBatch);
            xoayNuoc = new CBusiXoayNuoc(Content, null, spriteBatch);
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            switch (CGlobalvareabal.Giaodiendangchon)
            {
                case GUIEnum.guiWelcome:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiWC = new guiWelcome(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiWC.Update(gameTime);
                    break;

                case GUIEnum.guiLevel:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiLV = new guiLevel(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiLV.Update(gameTime);
                    break;               

                case GUIEnum.guiAmLuong:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiAL = new guiAmLuong(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiAL.Update(gameTime);
                    break;

                case GUIEnum.guiAmThanh:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiAT = new guiAmThanh(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiAT.Update(gameTime);
                    break;

                case GUIEnum.guiCaiDat:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiCD = new guiCaiDat(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiCD.Update(gameTime);
                    break;

                case GUIEnum.guiDatTen:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiDT = new guiDatTen(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiDT.Update(gameTime);
                    break;

                case GUIEnum.guiManHinhNhacNen:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiNN = new guiManHinhNhacNen(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiNN.Update(gameTime);
                    break;

                case GUIEnum.guiManHinhThoat:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiThoat = new guiManHinhThoat(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiThoat.Update(gameTime);
                    break;

                case GUIEnum.guiMapGamePlay:
                    if (CGlobalvareabal.Chuyengiaodien == true)
                    {
                        guiMap = new guiMapGamePlay(Content, null, spriteBatch);
                        CGlobalvareabal.Chuyengiaodien = false;
                    }
                    guiMap.Update(gameTime);
                    break;

                case GUIEnum.guiThoat:
                    this.Exit();
                    break;
            }

            bomNo.Update(gameTime);
            hoiMau.Update(gameTime);
            lamCham.Update(gameTime);
            noKiem.Update(gameTime);
            samSet.Update(gameTime);
            suTuHong.Update(gameTime);
            thienThach.Update(gameTime);
            troi.Update(gameTime);
            xoayNuoc.Update(gameTime);

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            switch (CGlobalvareabal.Giaodiendangchon)
            {
                case GUIEnum.guiLevel:
                    guiLV.Draw(gameTime);
                    break;

                case GUIEnum.guiWelcome:
                    guiWC.Draw(gameTime);
                    break;

                case GUIEnum.guiAmLuong:
                    guiAL.Draw(gameTime);
                    break;

                case GUIEnum.guiAmThanh:
                    guiAT.Draw(gameTime);
                    break;

                case GUIEnum.guiCaiDat:
                    guiCD.Draw(gameTime);
                    break;

                case GUIEnum.guiDatTen:
                    guiDT.Draw(gameTime);
                    break;

                case GUIEnum.guiManHinhNhacNen:
                    guiNN.Draw(gameTime);
                    break;

                case GUIEnum.guiManHinhThoat:
                    guiThoat.Draw(gameTime);
                    break;

                case GUIEnum.guiMapGamePlay:
                    guiMap.Draw(gameTime);
                    break;
            }

            bomNo.Draw(gameTime);
            hoiMau.Draw(gameTime);
            lamCham.Draw(gameTime);
            noKiem.Draw(gameTime);
            samSet.Draw(gameTime);
            suTuHong.Draw(gameTime);
            thienThach.Draw(gameTime);
            troi.Draw(gameTime);
            xoayNuoc.Draw(gameTime);

            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
